package jj.fly.activiti.leavebill.controller;

import jj.fly.activiti.leavebill.domain.Employee;
import jj.fly.activiti.leavebill.service.IEmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: jiangjun
 * Date: 2018/5/8
 * Time: 下午5:09
 * Description:
 */
@Controller
public class LoginController {

    @Resource
    private IEmployeeService employeeService;

    @GetMapping("/toLoginUI")
    public String toLoginUI(){
        return "login";
    }

    @PostMapping(value = "/login2")
    public String login(@RequestBody Employee employee){
        String name = employee.getName();
        return "index";
    }

    @RequestMapping (value = "/login")
    public String login(HttpServletRequest request, Model model, String name) {
        Employee employee = employeeService.getDataByName(name);
        request.getSession().setAttribute("current_user", employee);

        // 测试数据
        List<String> userList=new ArrayList<String>();
        userList.add("admin");
        userList.add("user1");
        userList.add("user2");
        model.addAttribute("userList", userList);

        model.addAttribute("current_user", employee);
        return "main";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("current_user");
        return "login";
    }

    @RequestMapping("/top")
    public String top(){
        return "top";
    }

    @RequestMapping("/left")
    public String left(){
        return "left";
    }

    @RequestMapping("/welcome")
    public String welcome(){
        return "welcome";
    }
}
